/* model */
import Field from '@model/entity/Field'
import { ConnectorBizTypeEnum, ConnectorModuleComponentNameEnum } from '@src/modules/connector/model'
/* mixin */
import { ConnectorModuleConnectorCreateMixin } from '@src/modules/connector/mixin'
/* vue */
import Component from 'vue-class-component'
/* util */
import Log from '@src/util/log'
import { initialize } from '@src/component/form/util'
import { packToForm } from '@src/modules/task/util/task'
import { isArray, isNotEmpty, isObject } from '@src/util/type'
import { getConnectorCreateDataSessionKeyByBizType } from '@src/modules/connector/util'

@Component({ 
  name: ConnectorModuleComponentNameEnum.ConnectorModuleConnectorCreateTaskMixin,
})
export default class ConnectorModuleConnectorCreateTaskMixin extends ConnectorModuleConnectorCreateMixin {
  
  /* 连接器新建数据 session 键 */
  public connectorCreateDataSessionKey = getConnectorCreateDataSessionKeyByBizType(ConnectorBizTypeEnum.Task)
  
  /* 混入的组件需要重写此属性 */
  private form: Record<string, any> = {}
  /* 混入的组件需要重写此属性 */
  private fields: Field[] = []
  
  /* 混入的组件需要重写此属性 */
  get fieldNameMap(): Record<string, any> {
    return {}
  }
  
  /** 
   * @description 连接器新建数据处理
  */
  public connectorInsertSelectCallDataHandler(data: Record<string, any>) {
    
    // 客户信息
    const customer = data?.customer || {}
    // 客户地址
    const customerAddress = customer?.customerAddress || {}
    // 客户联系人
    const customerLinkman = customer?.linkman || {}
    // 工程师资质
    const engineerQualification = data?.engineerQualification || ''
    // 产品
    const products: Record<string, any>[] = isArray(data?.products) ? data.products : isObject(data?.products) ? [data.products] : []
    
    const attribute = {
      ...data
    }
    
    const packData = {
      ...data,
      attribute,
      taddress: customerAddress,
      tlmId: customerLinkman?.id || '',
      tlmName: customerLinkman?.name || '',
      tlmPhone: customerLinkman?.phone || '',
      products,
    }
    
    let form = packToForm(this.fields, packData)
    
    // 工程师资质
    form.engineerQualification = engineerQualification ? [engineerQualification] : []
    
    Log.info(form, 'form', this.connectorInsertSelectCallDataHandler.name)
    
    const value = initialize(this.fields, form)
    
    Log.info(value, 'value', this.connectorInsertSelectCallDataHandler.name)
    
    setTimeout(() => {
      
      const newValue = {
        ...this.form,
        ...value
      }
      
      Log.info(newValue, 'newValue', this.connectorInsertSelectCallDataHandler.name)
      
      Object.keys(newValue).forEach(key => {
        const value = newValue[key]
        this.$set(this.form, key, value)
      })
      
      // 连接器新建的工单不允许修改工单类型
      const taskNoField = this.fieldNameMap.taskNo || {}
      taskNoField.disabled = true
      
      this.$refs?.form?.rerenderFormBuilder()
      
      if (isNotEmpty(products)) {
        this.$refs?.form?.updateProductForProductSelect(products)
      }
      
    }, 300)
    
  }
  
  /**
   * @description 消息处理后的事件
  */
  public onFrameMessageAfterHandler() {
    setTimeout(() => {
      this.fetchConnectorInsertSelectCall()
    }, 300)
  }
  
}

